import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy, ViewChild} from '@angular/core';
import {TrainService} from "app/pages/train/train.service";

import {ActivatedRoute, Params, Router} from "@angular/router";
import {SeedInfiniteScroll} from "app/shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {ToolsService} from "@shared/service/tools/tools.service";

import {StudentScheduleCourseVO} from "../../../../interface";

@Component({
  selector: 'lesson-list',
  templateUrl: './lesson-list.component.html',
  styleUrls: ['./lesson-list.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class LessonListComponent implements OnInit {
  public lessons: Array<StudentScheduleCourseVO> = [];
  public stateStore = {
    haveData: true
  };
  public dataStore = {
    totalLessons: 0,
    className: '',
    studentName: '',
    identityType: '' // 身份类型
  };
  public studentInfo = { // 教练端
    classId: 0,
    courseId: 0,
    studentId: 0,
    page: 1,
    rows: 10
  };
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(private  trainService: TrainService,
              private route: ActivatedRoute,
              private router: Router,
              private tools: ToolsService) {
/*    this.route.params.subscribe((params: Params) => {
      this.dataStore.identityType = params['identityType'];
    });
    this.route.queryParams.subscribe((params: Params) => {
      this.studentInfo.classId = params['classId'];
      this.studentInfo.studentId = params['studentId'];
      this.studentInfo.courseId = params['courseId'];
      this.dataStore.studentName = params['name'];
    });*/
    this.route.params
      .switchMap((params: Params) => {
        this.dataStore.identityType = params['identityType'];
        return this.route.queryParams;
      }).subscribe((params: Params) => {
      this.studentInfo.classId = params['classId'];
      this.studentInfo.studentId = params['studentId'];
      this.studentInfo.courseId = params['courseId'];
      this.dataStore.studentName = params['name'];
    });
  }

  /**
   * @description 根据身份请求相应方法
   * @author ouyqx-691
   * @date 2017-1-5
   */
  getLessonListForIdentityType() {
    if (this.dataStore.identityType === 'teacher') {
      this.getLessonListForTeacher();
    } else {
      this.getLessonListForParent();
    }
  }

  /**
   * @description 根据学员信息查询课时列表(教练)
   * @author ouyqx-691
   * @date 2017-12-31
   */
  getLessonListForTeacher() {
    this.trainService.getLessonListForTeacher(this.studentInfo).subscribe((res) => {
      if (res.result === '0' && res.data) {
        const lessons = res.data.list;
        this.studentInfo.page++;
        this.infiniteScroll.complete();
        this.lessons = [...this.lessons, ...lessons];
        this.dataStore.totalLessons = res.data.total;
        if (!res.data.hasNextPage) {
          this.infiniteScroll.enable(false);
          if (lessons.length === 0) {
            return this.stateStore.haveData = false;
          }
        }
      }
    });
  }

  /**
   * @description 查询课时列表（家长）
   * @author ouyqx-691
   * @date 2017-12-31
   */
  getLessonListForParent() {
    const {classId, studentId, page, rows} = this.studentInfo;
    const params = {
      classId: classId,
      studentId: studentId,
      page: page,
      rows: rows
    };
    this.trainService.getLessonListForParent(params).subscribe((res) => {
      if (res.result === '0' && res.data) {
        const lessons = res.data.list;
        this.studentInfo.page++;
        this.infiniteScroll.complete();
        this.lessons = [...this.lessons, ...lessons];
        this.dataStore.totalLessons = res.data.total;
        if (!res.data.hasNextPage) {
          this.infiniteScroll.enable(false);
          if (lessons.length === 0) {
            return this.stateStore.haveData = false;
          }
        }
      }
    });
  }

  /**
   * @description 删除点评（家长）
   * @author ouyqx-691
   * @date 2017-1-5
   */
  updateRemarkReadState(lesson) {
    const params = {
      scheduleId: lesson.scheduleId,
      studentId: this.studentInfo.studentId
    };
    this.trainService.updateRemarkReadState(params).subscribe((res) => {
    });
  }

  /**
   * @description 前往点评详情
   * @author ouyqx-691
   * @date 2017-12-31
   */
  goToRemarkDetail(lesson) {
    const {classId, studentId} = this.studentInfo;
    // 家长端
    if (this.dataStore.identityType === 'parent') {
      if (lesson.commentState === 0) {
        this.tools.showToast('教练还未点评该课时哦！');
      } else {
        const queryParams = {
          studentId: studentId,
          lessonId: lesson.scheduleId
        };
        this.updateRemarkReadState(lesson);
        this.router.navigate(['train/remark/parent/lesson'], {queryParams});
      }
      return;
    }
    // 学生评论状态(0:未开课，1:未点评，2:已点评，3：缺勤，4：请假),
    const queryParams = {
      classId: classId,
      studentId: studentId,
      lessonId: lesson.id,
      name: this.dataStore.studentName
    };
    if (lesson.status === 0) {
      this.tools.showToast('该课还未上，不可写点评');
    } else if (lesson.status === 1) {
      this.router.navigate(['train/remark/teacher/lesson'], {queryParams});
    } else if (lesson.status === 2) {
      Object.assign(queryParams, {
        remarkId: lesson.commentId
      });
      this.router.navigate(['train/remark/teacher/lesson'], {queryParams});
    } else if (lesson.status === 3) {
      this.tools.showToast('学员缺勤，不可写点评');
    } else if (lesson.status === 4) {
      this.tools.showToast('学员请假，不可写点评');
    }

  }


  ngOnInit() {
    this.getLessonListForIdentityType();
  }
}
